package com.example.recyclerdemo.room;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Database(entities = {StudentEntity.class},version = 1,exportSchema = false)
public abstract class StudentRoomDatabase extends RoomDatabase {
    public  static String DB_NAME="stu_info";
    private static  volatile StudentRoomDatabase INSTANCE;
    private static final int NUMBER_OF_THREADS=4;

    public  abstract StudentRoomDao getStudentRoomDao();
    public static final ExecutorService writeExecutor =
            Executors.newFixedThreadPool(NUMBER_OF_THREADS);

    //单例模式
    public static StudentRoomDatabase getInstance(final Context context){
        if (INSTANCE ==null){
            synchronized (StudentRoomDatabase.class){
                if (INSTANCE == null){
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            StudentRoomDatabase.class, StudentRoomDatabase.DB_NAME)
                            .allowMainThreadQueries()
                            .build();
                }
            }
        }
        return INSTANCE;
    }
}
